package com.itic.hpp.order;

import com.itic.appbase.applications.redis.IRedisDao;
import com.itic.appbase.framework.Base.BaseCode;
import com.itic.appbase.framework.type.OrderState;
import com.itic.appbase.framework.type.RedisKey;
import com.itic.appbase.framework.type.SendMethod;
import com.itic.mobile.base.BaseMobile;
import com.itic.system.order.order.persistence.model.Order;
import com.itic.system.order.order.persistence.model.OrderExample;
import com.itic.system.order.order.service.OrderService;
import com.itic.system.vo.OrderConditionVo;
import com.itic.system.vo.OrderDetailVo;
import com.itic.system.vo.OrderListVo;
import com.itic.system.vo.SubmitOrderVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/hpp/order")
public class OrderInterface extends BaseMobile {

    @Autowired
    private OrderService orderService;
    @Autowired
    private IRedisDao<String,Object> redisDao;

    /**
     *提交拼团并且支付
     * @param request
     * @param response
     * @param submitOrderVo
     * @return
     */
    @RequestMapping(value="/submit")
    @ResponseBody
    public Map<String,Object> submitOrder(HttpServletRequest request, HttpServletResponse response,@RequestBody SubmitOrderVo submitOrderVo){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            resultMap =  orderService.submitOrder(submitOrderVo);
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "submitOrder", e.toString(),"参与拼团接口",e);
        }finally {
            StringBuffer keyLock = new StringBuffer(RedisKey.SubmitOrderLock.getKey()).append(submitOrderVo.getCollageId());
            this.redisDao.delete(keyLock.toString());
        }
        return resultMap;
    }

    /**
     * 我的订单(我是团员时)
     * @param request
     * @param response
     * @param state
     * @param userId
     * @return
     */
    @RequestMapping(value="/myOrderList")
    @ResponseBody
    public Map<String,Object> myOrderList(HttpServletRequest request, HttpServletResponse response,String state,String userId){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            OrderState orderState = null;
            if(StringUtils.isNoneBlank(userId,state) && (orderState = OrderState.getOrderState(state))!=null){
                OrderExample example = new OrderExample();
                this.handlePageQueryDefault(request,example);
                List<OrderListVo> orderListVos = this.orderService.myOrderList(orderState,userId,example);
                resultMap.put(BaseCode.DATA,orderListVos);
                resultMap.put(BaseCode.CODE,BaseCode.SUCCESS_ERROR_CODE);
                resultMap.put(BaseCode.MSG,BaseCode.SUCCESS_ERROR_MSG);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "myOrderList", e.toString(),"(我是团员)我的订单列表接口",e);
        }
        return resultMap;
    }

    /**
     * 我是团长我的订单列表
     * @param request
     * @param response
     * @param state 订单状态
     * @param userId 用户Id
     * @return
     */
    @RequestMapping(value="/myCollageOrderList")
    @ResponseBody
    public Map<String,Object> myCollageOrderList(HttpServletRequest request, HttpServletResponse response,String state,String userId){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            OrderState orderState = null;
            if(StringUtils.isNoneBlank(userId,state) && (orderState = OrderState.getOrderState(state))!=null){
                OrderExample example = new OrderExample();
                this.handlePageQueryDefault(request,example);
                List<OrderListVo> orderListVos = this.orderService.myCollageOrderList(orderState,userId,example);
                resultMap.put(BaseCode.DATA,orderListVos);
                resultMap.put(BaseCode.CODE,BaseCode.SUCCESS_ERROR_CODE);
                resultMap.put(BaseCode.MSG,BaseCode.SUCCESS_ERROR_MSG);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "myCollageOrderList", e.toString(),"(我是团长)我的订单列表接口",e);
        }
        return resultMap;
    }

    @RequestMapping(value="/detail")
    @ResponseBody
    public Map<String,Object> detail(HttpServletRequest request, HttpServletResponse response,String orderNo){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(StringUtils.isNotEmpty(orderNo)){
                OrderDetailVo orderDetailVo = this.orderService.orderDetail(orderNo);
                resultMap.put(BaseCode.DATA,orderDetailVo);
                resultMap.put(BaseCode.CODE,BaseCode.SUCCESS_ERROR_CODE);
                resultMap.put(BaseCode.MSG,BaseCode.SUCCESS_ERROR_MSG);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "detail", e.toString(),"订单详情页接口",e);
        }
        return resultMap;
    }

    /**
     * 后台管理搜索订单列表接口
     * @param request
     * @param response
     * @param orderConditionVo
     * @return
     */
    @RequestMapping(value="manageOrderList")
    @ResponseBody
    public Map<String,Object> manageOrderList(HttpServletRequest request, HttpServletResponse response, OrderConditionVo orderConditionVo){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(StringUtils.isNotEmpty(orderConditionVo.getUserId())){
                OrderExample example = new OrderExample();
                this.handlePageQueryDefault(request,example);
                resultMap = this.orderService.manageOrderList(orderConditionVo,example);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "manageOrderList", e.toString(),"后台管理订单列表接口",e);
        }
        return resultMap;
    }

    /**
     * 确认收货
     * @param request
     * @param response
     * @param orderNo
     * @param userId
     * @return
     */
    @RequestMapping(value="confirmRecive")
    @ResponseBody
    public Map<String,Object> confirmRecive(HttpServletRequest request, HttpServletResponse response,String orderNo,String userId){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(StringUtils.isNoneBlank(orderNo,userId)){
                resultMap = this.orderService.confirmRecive(orderNo,userId);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "confirmRecive", e.toString(),"确认收货接口",e);
        }
        return resultMap;
    }

    /**
     * 发货
     * @param request
     * @param response
     * @param orderNo
     * @param userId
     * @param expressCode
     * @param expressNo
     * @param method
     * @return
     */
    @RequestMapping(value="deliverGood")
    @ResponseBody
    public Map<String,Object> deliverGood(HttpServletRequest request, HttpServletResponse response,String orderNo,String userId,String expressCode,String expressNo,String method){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            SendMethod sendMethod =null;
            if(StringUtils.isNoneBlank(orderNo,userId ,method) && (sendMethod = SendMethod.getSendMethod(method))!=null){
                resultMap = this.orderService.deliverGood(orderNo,userId,sendMethod,expressCode,expressNo);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "deliverGood", e.toString(),"发货接口",e);
        }
        return resultMap;
    }

    /**
     * 申请退款业务
     * @param request
     * @param response
     * @param orderNo
     * @param userId
     * @return
     */
    @RequestMapping(value="applyAfterSale")
    @ResponseBody
    public Map<String,Object> applyAfterSale(HttpServletRequest request, HttpServletResponse response,String orderNo,String userId){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(StringUtils.isNoneBlank(orderNo,userId)){
                resultMap = this.orderService.applyOrderAfterSale(orderNo,userId);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "applyAfterSale", e.toString(),"申请售后接口",e);
        }
        return resultMap;
    }

    /**
     * 同意退款
     * @param request
     * @param response
     * @param orderNo
     * @param userId
     * @return
     */
    @RequestMapping(value="agreeAfterSale")
    @ResponseBody
    public Map<String,Object> agreeAfterSale(HttpServletRequest request, HttpServletResponse response,String orderNo,String userId){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(StringUtils.isNoneBlank(orderNo,userId)){
                resultMap = this.orderService.agreeAfterSale(orderNo,userId);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "agreeAfterSale", e.toString(),"同意退款接口",e);
        }
        return resultMap;
    }

    /**
     *  订单查看物流信息
     * @param orderNo
     * @return
     */
    @RequestMapping(value="express/info")
    @ResponseBody
    public Map<String,Object> expressInfo(String orderNo){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(StringUtils.isNoneBlank(orderNo)){
                resultMap = this.orderService.expressInfo(orderNo);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "agreeAfterSale", e.toString(),"同意退款接口",e);
        }
        return resultMap;
    }

}
